﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ISS.IR.RRNMS.Library.DataAccess
{
    public static partial class Main
    {
        public static Guid Login(string Username, string Password)
        {
            Guid UserId = Guid.Empty;

            try
            {
                command.CommandText = "usp_login";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Clear();

                command.Parameters.Add(new SqlParameter("Username", Username));
                command.Parameters.Add(new SqlParameter("Password", Password));

                OpenConnection();
                object objUserId = command.ExecuteScalar();
                CloseConnection();

                if (objUserId != DBNull.Value && objUserId != null)
                    UserId = Guid.Parse(objUserId.ToString());

            }
            catch (Exception exc) { CloseConnection(); }

            return UserId;
        }

        public static BusinessLogic.UserPermissions SelectUserPermissions(Guid UserId)
        {
            BusinessLogic.UserPermissions Permissions = new BusinessLogic.UserPermissions();

            try
            {
                command.CommandText = "usp_SelectUserPermissions";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Clear();

                command.Parameters.Add(new SqlParameter("UserId", UserId));
                DataTable dtPermissions = new DataTable();

                adapter.Fill(dtPermissions);

                Permissions.HasAccountPerm = Convert.ToBoolean(dtPermissions.Rows[0]["HasAccountPerm"]);
                Permissions.HasCalendarPerm = Convert.ToBoolean(dtPermissions.Rows[0]["HasCalendarPerm"]);
                Permissions.HasSearchPerm = Convert.ToBoolean(dtPermissions.Rows[0]["HasSearchPerm"]);
                Permissions.IsAdmin = Convert.ToBoolean(dtPermissions.Rows[0]["IsAdmin"]);
            }
            catch (Exception exc)
            { }

            return Permissions;
        }

        public static List<Item> SelectAllUsers()
        {
            command.CommandText = "usp_SelectAllUsers";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            DataTable dtUsers = new DataTable();

            adapter.Fill(dtUsers);

            List<Item> Users = new List<Item>();

            try
            {
                for (int i = 0; i < dtUsers.Rows.Count; i++)
                {
                    Users.Add(
                        new Item()
                        {
                            GUID = Guid.Parse(dtUsers.Rows[i]["UserId"].ToString()),
                            Value = dtUsers.Rows[i]["FullName"].ToString()
                        }); ;
                }
            }
            catch (Exception exc)
            { }

            return Users;
        }

        public static List<Item> SelectAllUserRoles()
        {
            DataTable dtRoles = DataAccess.Main.SelectItemsByCategory(6);

            List<Item> Roles = new List<Item>();

            try
            {
                for (int i = 0; i < dtRoles.Rows.Count; i++)
                {
                    Roles.Add(
                        new Item()
                        {
                            Id = Convert.ToInt32(dtRoles.Rows[i]["Id"].ToString()),
                            Value = dtRoles.Rows[i]["Value"].ToString()
                        }); ;
                }
            }
            catch (Exception exc)
            { }

            return Roles;
        }
        public static BusinessLogic.IR_User SelectOneUser(Guid UserId)
        {
            try
            {
                command.CommandText = "usp_SelectOneUser";
                command.Parameters.Clear();
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("UserId", UserId));

                DataTable dtUser = new DataTable();

                adapter.Fill(dtUser);

                BusinessLogic.IR_User user = new BusinessLogic.IR_User();

                DataRow drUser = dtUser.Rows[0];

                user.UserId = Guid.Parse(drUser["UserId"].ToString());
                user.FullName = drUser["FullName"].ToString();
                user.Username = drUser["Username"].ToString();
                user.Password = drUser["Password"].ToString();
                user.Telephone = drUser["Telephone"].ToString();
                user.Mobile = drUser["Mobile"].ToString();

                if (drUser["DefaultRoleId"] != DBNull.Value)
                    user.DefaultRole = new Item()
                    {
                        Id = Convert.ToInt32(drUser["DefaultRoleId"].ToString()),
                        Value = drUser["DefaultRoleName"].ToString()
                    };

                if (drUser["ManagerId"] != DBNull.Value)
                    user.Manager = new Item()
                    {
                        GUID = Guid.Parse(drUser["ManagerId"].ToString()),
                        Value = drUser["ManagerName"].ToString()
                    };

                user.Permissions = new BusinessLogic.UserPermissions()
                {
                    HasAccountPerm = Convert.ToBoolean(drUser["HasAccountPerm"]),
                    HasCalendarPerm = Convert.ToBoolean(drUser["HasCalendarPerm"]),
                    HasSearchPerm = Convert.ToBoolean(drUser["HasSearchPerm"]),
                    IsAdmin = Convert.ToBoolean(drUser["IsAdmin"])
                };
                return user;
            }
            catch (Exception exc)
            {

            }
            return new BusinessLogic.IR_User();
        }

        public static void SaveUser(BusinessLogic.IR_User user)
        {
            try
            {
                command.CommandText = "usp_SaveUser";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Clear();

                command.Parameters.Add(new SqlParameter("FullName", user.FullName));
                command.Parameters.Add(new SqlParameter("Username", user.Username));
                command.Parameters.Add(new SqlParameter("Password", user.Password));
                command.Parameters.Add(new SqlParameter("Telephone", user.Telephone));
                command.Parameters.Add(new SqlParameter("Mobile", user.Mobile));
                command.Parameters.Add(new SqlParameter("Email", user.Email));
                command.Parameters.Add(new SqlParameter("UserId", user.UserId));

                if (user.DefaultRole.Id == 0)
                    command.Parameters.Add(new SqlParameter("DefaultRoleId", DBNull.Value));
                else
                    command.Parameters.Add(new SqlParameter("DefaultRoleId", user.DefaultRole.Id));

                if (user.Manager.GUID == Guid.Empty)
                    command.Parameters.Add(new SqlParameter("ManagerId", DBNull.Value));
                else
                    command.Parameters.Add(new SqlParameter("ManagerId", user.Manager.GUID));

                command.Parameters.Add(new SqlParameter("HasAccountPerm", user.Permissions.HasAccountPerm));
                command.Parameters.Add(new SqlParameter("HasCalendarPerm", user.Permissions.HasCalendarPerm));
                command.Parameters.Add(new SqlParameter("HasSearchPerm", user.Permissions.HasSearchPerm));
                command.Parameters.Add(new SqlParameter("IsAdmin", user.Permissions.IsAdmin));

                OpenConnection();
                command.ExecuteNonQuery();
                CloseConnection();
            }
            catch (Exception exc)
            { }
        }



        internal static void DeleteUser(Guid UserId)
        {
            command.CommandText = "usp_DeleteUser";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("UserId", UserId));

            OpenConnection();
            command.ExecuteNonQuery();
            CloseConnection();
        }

    }

}
